//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.InvCountListPageController');
	com.yonyou.ncmm_pac_pda.InvCountListPageController = function() {
		com.yonyou.ncmm_pac_pda.InvCountListPageController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$initialize() {
		//you can programing by $ctx API
		//get the context data through $ctx.get()
		//set the context data through $ctx.push(json)
		//set the field of the context through $ctx.put(fieldName, fieldValue)
		//get the parameter of the context through $ctx.param(parameterName)
		//Demo Code:
		//    var str = $ctx.getString();      //获取当前Context对应的字符串
		//    alert($ctx.getString())          //alert当前Context对应的字符串
		//    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
		//    json["x"] = "a";        //为当前json增加字段
		//    json["y"] = [];           //为当前json增加数组
		//    $ctx.push(json);            //设置context，并自动调用数据绑定
		//
		//    put方法需手动调用databind()
		//    var x = $ctx.get("x");    //获取x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.put("x", "b");     //设置x字段值
		//    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
		//    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
		//    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
		//    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组

		//your initialize code below...

	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$evaljs(js) {
		eval(js)
	}

	var popDialogInfo = {
		"viewid": "panel9", //显示的控件的id，通常是一个容器控件div
		"popgravity": "popbottom", //popcenter|poptop|popbottom|popleft|popright,弹出后最终显示的位置
		// istouch : 'false'
	};

	// 由列表跳转明细页面
	function com$yonyou$ncmm_pac_pda$InvCountListPageController$onitemclick(sender, args) {
		// 获取当前行内容
		/*var row = $id("listviewdefine0").get("row");
		 row = $stringToJSON(row);
		 // 单击行跳转到详情页面
		 $view.open({
		 "viewid" : "com.yonyou.ncmm_pac_pda.InvCountDetailPage",
		 "isKeep" : "true",
		 "row" : row
		 });
		 */
		$view.openPop(popDialogInfo);
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$button0_onclick(sender, args) {
		// 返回即关闭当前页面
		$view.close();

	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$today_onclick(sender, args) {
		// 按照默认查询条件查询当天生产订单数据
		var condition = {};
		// 查询的日期显示是日期格式，其实是日期时间格式
		var fromdate = NCMMPubUtil.getNowDate().toString() + " 00:00:00 ";
		var todate = NCMMPubUtil.getNowDate().toString() + " 23:59:59 ";
		condition = {
			"dbilldate": "between '" + fromdate + "' and '" + todate + "'"
		}
		downloadAndQueryData(condition);
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$week_onclick(sender, args) {
		// 默认查询本周生产订单数据
		var condition = {};
		// 查询的日期显示是日期格式，其实是日期时间格式
		// 查询的日期显示是日期格式，其实是日期时间格式
		var fromdate = NCMMDateUtil.getWeekDate().beginDate;
		var todate = NCMMDateUtil.getWeekDate().endDate;
		condition = {
			"dbilldate": "between '" + fromdate + "' and '" + todate + "'"
		}
		downloadAndQueryData(condition);
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$month_onclick(sender, args) {
		// 默认查询本月生产订单数据
		var condition = {};
		// 查询的日期显示是日期格式，其实是日期时间格式
		var fromdate = NCMMDateUtil.getMonthDate().beginDate;
		var todate = NCMMDateUtil.getMonthDate().endDate;
		condition = {
			"dbilldate": "between '" + fromdate + "' and '" + todate + "'"
		}
		downloadAndQueryData(condition);
	}

	// 从NC端下载数据到本地数据库再查出数据进行加载
	function downloadAndQueryData(condition) {
		var defcondition = INVCOUNTUtil.getDefaultCondition();
		if(defcondition == null) {
			// 不设置库存组织和仓库没有业务意义
			return;
		}
		var json = {};
		if(NCMMPubUtil.isNotEmpty(defcondition["stordoc"])) {
			json["storcode"] = " = '" + defcondition["stordoc"] + "'";
		}
		json["pk_org_h"] = " = '" + defcondition["stockorg"] + "'";
		condition = $.extend({}, condition, json);
		INVCOUNTUtil.processBillDataFromNC(INVCOUNTVO, "库存盘点单", condition, function() {
			queryDataFromLocalDB();
		});
	}

	// 从本地数据库查询数据进行页面加载
	function queryDataFromLocalDB() {
		if(NCMMPubUtil.isTableExist(INVCOUNTVO) == false) {
			return;
		}
		//按单据号+行号为序，升序排列
		var wheresql = "order by vbillcode , crowno";
		NCMMPubUtil.queryVO(INVCOUNTVO, wheresql, function(data) {
			if(data == null || data == "" || data == "[]" || data.length == 0) {
				$ctx.push({});
				$toast("本地没有订单记录！");
				return;
			}
			//处理特殊符号反斜杠
			data = NCMMPubUtil.htmlspecialjsonarray2UI(data);
			var result = {
				orders: eval(data)
			};
			//--Begin--处理单据列表界面的精度---
			var numkeys = new Array("nonhandnum", "scannum");
			INVCOUNTUtil.processScale(result.orders, numkeys, "pk_measdoc");
			var astnumkeys = new Array("nonhandastnum");
			INVCOUNTUtil.processScale(result.orders, astnumkeys, "castunitid");
			//---End---------
			$ctx.push(result);
			NCMMPubUtil.notifyFilter("search0", "listviewdefine0", ["vbillcode", "cinvname", "nonhandnum", "nonhandastnum", "scannum"]);
		});
	}

	function deleteRow(row) {
		if(row["scannum"] != null && row["scannum"] != "") {
			$alert("该单据存在扫描记录，不能删除！请先删除扫描记录！")
			return;
		}
		// 从本地数据删除，然后重新查询数据进行加载
		NCMMPubUtil.deleteVO(INVCOUNTVO, row);
		queryDataFromLocalDB();
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$onload(sender, args) {
		// onloadControlForList();
		// 加载数据的时候判断如果有表则查询，如果不存在数据表则先创建
		NCMMPubUtil.createTable(INVCOUNTVO);
		queryDataFromLocalDB();

	}

	function onloadControlForList() {
		var rightflipper = [{
			"halign": "center",
			"text-align": "right",
			"width": "70",
			"icon-width": "12",
			"istogglebutton": "false",
			"font-pressed-color": "#ffffff",
			"icon-height": "12",
			"height": "59",
			"color": "#ffffff",
			"layout-type": "linear",
			"background": "#FF615A",
			"font-size": "12",
			"value": "删除",
			"font-family": "default",
			"valign": "center",
			"checked": "false",
			"icon-text-spacing": "8",
			"onclick": "onItemDelete()"
		}, {
			"halign": "center",
			"text-align": "right",
			"width": "70",
			"icon-width": "12",
			"istogglebutton": "false",
			"font-pressed-color": "#ffffff",
			"icon-height": "12",
			"height": "59",
			"color": "#ffffff",
			"layout-type": "linear",
			"background": "#00ff40",
			"font-size": "12",
			"value": "采集",
			"font-family": "default",
			"valign": "center",
			"checked": "false",
			"icon-text-spacing": "8",
			"onclick": "onItemCollect()"
		}];
		$id("listviewdefine0").set("slidingitems", rightflipper);
	}

	// 列表项左划删除事件
	function onItemDelete() {
		if($confirm("确认要删除当前行数据么？")) {
			var row = $id("listviewdefine0").get("row");
			deleteRow($stringToJSON(row));
		}
	}

	// 列表项左划采集事件
	function onItemCollect() {
		var row = $id("listviewdefine0").get("row");
		// 采集打开扫条码页面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.InvCountScanPage",
			isKeep: "true",
			"row": row
		})
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$imagebutton0_onclick(sender, args) {
		$menu.openDropDownList({
			"controlid": "imagebutton0",
			"dropDownListWidth": "100", //菜单项的宽度
			"background": "#f2f2f2",
			//"background-image":"app.png",
			"color": "#ee4311",
			"font-size": "12",
			"halign": "center", //right  center
			"split-color": "#ee4311",
			//"margin-left":"10",
			"panelstyle": "round-div",
			"border-color": "#ee4311",
			"dropItemsArray": [{
				"name": "主界面",
				"action": "menu1_click()"
			}, {
				"name": "数据管理",
				"action": "menu2_click()"
			}, {
				"name": "业务设置",
				"action": "menu3_click()"
			}, {
				"name": "注销",
				"action": "menu4_click()"
			}, {
				"name": "扫描记录",
				"action": "menu5_click()"
			}]
		})
	}

	function menu1_click() {
		// 跳转到主界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.HomePage", //主界面id
			isKeep: "false" //
		})
	}

	function menu2_click() {
		// 跳转到数据管理界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.DM_main",
			isKeep: "false"
		})
	}

	function menu3_click() {
		// 跳转到业务设置界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.BusinessSettingPage",
			isKeep: "false"
		})
	}

	function menu4_click() {
		// 跳转到登录界面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.LoginPage", //主界面id
			isKeep: "false"
		})
	}

	function menu5_click() {
		// 扫描记录打开扫描记录列表画面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.InvCountScanListPage",
			isKeep: "true"
		})
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$button4_onclick(sender, args) {
		var row = $id(sender).get("row");
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.InvCountScanPage",
			isKeep: "true",
			"row": row,
			"flag": 1
		})

	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$button5_onclick(sender, args) {
		// 扫描记录打开扫描记录列表画面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.InvCountScanListPage",
			isKeep: "true"
		})

	}
	
	//手工录入单据码或者红外扫码
	function scanonchange(sender, args) {
	    myCallBack();
	    $ctx.put("code","");
	    $ctx.dataBind();
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$imagebutton1_onclick(sender, args) {
		$scanner.open({
			bindfield: "code",
			callback: "myCallBack()"
		});
	}

	function myCallBack() {
		// 扫条码调到采集页面
		var code = $ctx.getString("code");
		// 判断单据码是否已经在本地数据库
		var condition = {
			"vbillcode": code
		};
		NCMMPubUtil.queryVOByConditions(INVCOUNTVO, condition, function(data) {
			if(typeof(data) == 'undefined' || data == null || data.length == 0 || eval(data).length < 1||NCMMPubUtil.isEmpty(data[0]["vbillcode"])) {
				$alert("未找到扫描的单据码，请在下载单据中确认！");
				return;
			} else {
				$view.open({
					viewid: "com.yonyou.ncmm_pac_pda.InvCountScanPage",
					isKeep: "true",
					code: code
				})
			}
		});
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$imagebutton2_onclick(sender, args) {
		var strcondition = $cache.read("pdcondition");
		//var condition = $stringToJSON(strcondition);
		// 点击查询按钮弹出查询条件画面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.InvCountQueryPage",
			isKeep: "true",
			"condition": strcondition,
			callback: function() {
				var qconditon = $param.getJSONObject("qcondition");
				downloadAndQueryData(qconditon);
			}
		})
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$checkAll(sender, args) {
		// 全选按钮逻辑
		$ctx.dataCollect();
		var isallchecked = $id(sender).get("checked");
		var orders = $ctx.getJSONArray("orders");
		if(orders == null)
			return;
		for(var i = 0; i < orders.length; i++) {
			orders[i]["ischecked"] = isallchecked;
		}

		var json = {};
		json["orders"] = orders;
		json["isallchecked"] = isallchecked;
		$ctx.push(json);

	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$checkSinge(sender, args) {
		// 单选按钮逻辑
		$ctx.dataCollect();
		var ischecked = $id(sender).get("checked");
		var isallchecked = true;
		if(ischecked == false) {
			isallchecked = false;
		} else {
			var orders = $ctx.getJSONArray("orders");
			for(var i = 0; i < orders.length; i++) {
				if(orders[i]["ischecked"] == null || orders[i]["ischecked"] == "false") {
					isallchecked = false;
					break;
				}
			}
		}
		var json = {};
		json["orders"] = orders;
		json["isallchecked"] = isallchecked;
		$ctx.push(json);
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$selectdata(sender, args) {
		// 按照查询条件进行快速查询
		NCMMPubUtil.quickFilter();

	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$pageRestart(sender, args) {
		// onloadControlForList();
		// 加载数据的时候判断如果有表则查询，如果不存在数据表则先创建
		NCMMPubUtil.createTable(INVCOUNTVO);
		queryDataFromLocalDB();
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Detail(sender, args) {
		// 详情
		$view.closePop(popDialogInfo);
		var row = $id("listviewdefine0").get("row");
		//row = $stringToJSON(row);
		// 单击行跳转到详情页面
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.InvCountDetailPage",
			"isKeep": "true",
			"row": row
		});
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Scan(sender, args) {
		// 采集
		$view.closePop(popDialogInfo);
		var row = $id("listviewdefine0").get("row");
		// 采集打开扫条码页面
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.InvCountScanPage",
			isKeep: "true",
			"row": row
		})
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Delete(sender, args) {
		// 删除
		$view.closePop(popDialogInfo);
		if($confirm("确认删除所选记录吗？")) {
			var row = $id("listviewdefine0").get("row");
			deleteRow($stringToJSON(row));
		}
	}

	function com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Cancel(sender, args) {
		// 取消
		$view.closePop(popDialogInfo);
	}

	com.yonyou.ncmm_pac_pda.InvCountListPageController.prototype = {
		onclick_Cancel: com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Cancel,
		onclick_Delete: com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Delete,
		onclick_Scan: com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Scan,
		onclick_Detail: com$yonyou$ncmm_pac_pda$InvCountListPageController$onclick_Detail,
		pageRestart: com$yonyou$ncmm_pac_pda$InvCountListPageController$pageRestart,
		selectdata: com$yonyou$ncmm_pac_pda$InvCountListPageController$selectdata,
		checkSinge: com$yonyou$ncmm_pac_pda$InvCountListPageController$checkSinge,
		checkAll: com$yonyou$ncmm_pac_pda$InvCountListPageController$checkAll,
		imagebutton2_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$imagebutton2_onclick,
		imagebutton1_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$imagebutton1_onclick,
		button5_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$button5_onclick,
		button4_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$button4_onclick,
		month_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$month_onclick,
		imagebutton0_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$imagebutton0_onclick,
		onload: com$yonyou$ncmm_pac_pda$InvCountListPageController$onload,
		week_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$week_onclick,
		today_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$today_onclick,
		button0_onclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$button0_onclick,
		onitemclick: com$yonyou$ncmm_pac_pda$InvCountListPageController$onitemclick,
		onload: com$yonyou$ncmm_pac_pda$InvCountListPageController$onload,
		initialize: com$yonyou$ncmm_pac_pda$InvCountListPageController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$InvCountListPageController$evaljs
	};
	com.yonyou.ncmm_pac_pda.InvCountListPageController.registerClass('com.yonyou.ncmm_pac_pda.InvCountListPageController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}